package com.tc.jgit;

import java.io.File;
import java.util.Collection;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.HttpConfig;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

public class GitUtil {
	//private static Log log = LogFactory.getLog(GitUtil.class);

	private GitUtil() {
	}

	public static Git getGit(String uri, String localDir, CredentialsProvider credentialsProvider) throws Exception {
		Git git = null;
		if (new File(localDir).exists() ) {
			git = Git.open(new File(localDir));
		} else {
			git = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setURI(uri)
					.setDirectory(new File(localDir)).call();
		}
		//设置一下post内存，否则可能会报错Error writing request body to server
		git.getRepository().getConfig().setInt(HttpConfig.HTTP, null, HttpConfig.POST_BUFFER_KEY, 512*1024*1024);
		return git;
	}

	public static CredentialsProvider getCredentialsProvider(String username, String password) {
		return new UsernamePasswordCredentialsProvider(username, password);
	}

	public static Repository getRepository(Git git) {
		return git.getRepository();
	}

	public static void pull(Git git, CredentialsProvider credentialsProvider) throws Exception {
		git.pull().setRemote("origin").setCredentialsProvider(credentialsProvider).call();
	}

	public static void push(Git git, CredentialsProvider credentialsProvider, String filepattern, String message)
			throws Exception {
		git.add().addFilepattern(filepattern).call();
		git.add().setUpdate(true);
		git.commit().setMessage(message).call();
		git.push().setCredentialsProvider(credentialsProvider).call();
	}

	public static void main(String[] args) throws Exception {
		String uri = "http://cai.tian@10.199.133.181:9999/vpal-new/beifu_payment/push-aml";
		String username = "cai.tian";
		String password = "m2KL23955Nh";
		String localDir = "D:\\Git\\push-aml";
		CredentialsProvider credentialsProvider = getCredentialsProvider(username, password);

		Git git = getGit(uri, localDir,credentialsProvider);
		//git
		Repository repository = git.getRepository();
		RevWalk revWalk = new RevWalk(repository);
        ObjectId commitId = repository.resolve("origin/release");
		revWalk.markStart(revWalk.parseCommit(commitId));
		for(RevCommit commit : revWalk) {
			System.out.println(commit.getFullMessage());
			System.out.println(commit.getId().getName());
		}

		/**
		ReflogReader release = repository.getReflogReader("origin/release");

		ReflogEntry lastEntry = release.getLastEntry();

		String comment = lastEntry.getComment();

		System.out.println(comment);*/

		//pull(git, credentialsProvider);
		//push(git, credentialsProvider, ".", "提交文件");
	}
}